package cn.com.thinvent.dispatch.service.biz.impl;

import cn.com.thinvent.dispatch.dao.HouseholdAssetsMapper;
import cn.com.thinvent.dispatch.entity.HouseholdAssets;
import cn.com.thinvent.dispatch.entity.response.PagedGridResult;
import cn.com.thinvent.dispatch.service.biz.HouseholdAssetsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class HouseholdAssetsServiceImpl implements HouseholdAssetsService {

    @Resource
    private HouseholdAssetsMapper householdAssetsMapper;

    @Override
    public PagedGridResult<HouseholdAssets> getAllHouseholdAssets(int page, int pageSize, String assetName) {
        int offset = (page - 1) * pageSize;
        List<HouseholdAssets> list = householdAssetsMapper.selectAllHouseholdAssets(assetName, offset, pageSize);
        int totalRecords = householdAssetsMapper.countAllHouseholdAssets(assetName);

        PagedGridResult<HouseholdAssets> pagedResult = new PagedGridResult<>();
        pagedResult.setPage(page);
        pagedResult.setTotal((int) Math.ceil((double) totalRecords / pageSize));
        pagedResult.setRecords(totalRecords);
        pagedResult.setRows(list);

        return pagedResult;
    }

    @Override
    public HouseholdAssets getHouseholdAssetsById(String id) {
        return householdAssetsMapper.selectHouseholdAssetsById(id);
    }

    @Override
    public void addHouseholdAssets(HouseholdAssets householdAssets) {
        householdAssetsMapper.insertHouseholdAssets(householdAssets);
    }

    @Override
    public void updateHouseholdAssets(HouseholdAssets householdAssets) {
        householdAssetsMapper.updateHouseholdAssets(householdAssets);
    }

    @Override
    public void deleteHouseholdAssets(String id) {
        householdAssetsMapper.deleteHouseholdAssets(id);
    }
}
